<template>
  <div class="app-container">
    <Ht-search-form
      v-show="showSearch"
      :dictMap="dictMapAsync"
      :queryParams="queryParams"
      :outputQueryParams="queryParams"
      :formOptions="formOptions"
      :defaultEventKey="defaultEventKey"
      :advOptions="[]"
    ></Ht-search-form>

    <!--    <div class="page-label-block">-->
    <!--      <span class="color-block">|</span>-->
    <!--      <span class="page-label-name">-->
    <!--      租户管理-->
    <!--      </span>-->
    <!--    </div>-->

    <ht-table
      ref="dataTable"
      :multi-select="false"
      :showSerialNumber="false"
      :dictMap="dictMap"
      :queryParams="queryParams"
      :tableBindColumns="tableBindColumns"
      :opButtonList="opButtonList"
      :reqConfig="regConfig"
      :defaultEventKey="defaultEventKey"
      :randomRowKey="true"
    >
      <template v-slot:operate>
        <el-table-column label="操作" align="left" width="50" fixed="right">
          <template slot-scope="scope">
            <ht-button-group
              :buttonList="[
                {
                  label: '明细',
                  doFunc: () => {
                    handleAction('detail', scope.row);
                  },
                },
              ]"
            />
          </template>
        </el-table-column>
      </template>
    </ht-table>

    <!-- 添加或修改弹出框 -->
    <el-dialog :title="title" center :visible.sync="open" width="1024px" append-to-body :close-on-click-modal="false">
      <el-form ref="detailForm" :model="detailForm" :rules="rules" label-width="120px">
        <el-card class="form-card msgGroup-card">
        <el-row type="flex">
          <el-col :span="12">
            <el-form-item label="交易流水号" prop="id">
              <el-input v-model="detailForm.id" :disabled="true" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="报文标识号" prop="pkgId">
              <el-input v-model="detailForm.pkgId" :disabled="true" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex">
          <el-col :span="12">
            <el-form-item label="CPG日期" prop="vdate">
              <el-date-picker
                v-model="detailForm.vdate"
                style="width: 100%"
                type="date"
                value-format="yyyyMMdd"
                :disabled="true"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工作日期" prop="workdate">
              <el-date-picker
                v-model="detailForm.workdate"
                style="width: 100%"
                type="date"
                value-format="yyyyMMdd"
                :disabled="true"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex">
          <el-col :span="12">
            <el-form-item label="报文类型" prop="pkgNo">
              <el-select
                style="width: 100%"
                v-model="
                  detailForm.pkgNo === null || detailForm.pkgNo === undefined
                    ? ''
                    : detailForm.pkgNo + '-' + detailForm.pkgNoDesc
                "
                placeholder="报文类型"
                :disabled="true"
              ></el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex">
          <el-col :span="12">
            <el-form-item label="发起直接参与机构号" prop="directSender">
              <el-input v-model="detailForm.directSender" :disabled="true" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="发起直接参与机构名" prop="directSenderName">
              <el-input v-model="detailForm.directSenderName" :disabled="true" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex">
          <el-col :span="12">
            <el-form-item label="接收直接参与机构号" prop="directRecver">
              <el-input v-model="detailForm.directRecver" :disabled="true" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="接收直接参与机构名" prop="directRecverName">
              <el-input v-model="detailForm.directRecverName" :disabled="true" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex">
          <el-col :span="12">
            <el-form-item label="查询账户数目" prop="acctCnt">
              <el-input v-model="detailForm.acctCnt" :disabled="true" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="通讯状态" prop="comStatus">
              <el-select style="width: 100%" v-model="detailForm.comStatus" placeholder="通讯状态" :disabled="true">
                <el-option
                  v-for="dict in dictMap.PKG_STATUS"
                  :key="dict.value"
                  :label="`${dict.value}-${dict.text}`"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex">
          <el-col :span="12">
            <el-form-item label="协议数目" prop="protoNum">
              <el-input v-model="detailForm.protoNum" :disabled="true" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="通讯状态" prop="comStatus">
              <el-select style="width: 100%" v-model="detailForm.comStatus" placeholder="通讯状态" :disabled="true">
                <el-option
                  v-for="dict in dictMap.PKG_STATUS"
                  :key="dict.value"
                  :label="`${dict.value}-${dict.text}`"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex">
          <el-col :span="12">
            <el-form-item label="业务状态" prop="txnStatus">
              <el-select style="width: 100%" v-model="detailForm.txnStatus" placeholder="业务状态" :disabled="true">
                <el-option
                  v-for="dict in dictMap.TXN_STATUS"
                  :key="dict.value"
                  :label="`${dict.value}-${dict.text}`"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="中心状态" prop="procStatus">
              <el-select style="width: 100%" v-model="detailForm.procStatus" placeholder="中心状态" :disabled="true">
                <el-option
                  v-for="dict in dictMap.PROCESSSTATUS"
                  :key="dict.value"
                  :label="`${dict.value}-${dict.text}`"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex">
          <el-col :span="12">
            <el-form-item label="报文接收时间" prop="rcvTime">
              <el-date-picker
                style="width: 100%"
                v-model="detailForm.rcvTime"
                value-format="yyyyMMddHHmmss"
                type="datetime"
                :disabled="true"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex">
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input type="textarea" v-model="detailForm.remark" :disabled="true" />
            </el-form-item>
          </el-col>
        </el-row>
          <div class="discountReceipt-pick-operate-box">
            <div class="operate-box-bottom">
              <div class="bottom-content">
                <ht-table
                  ref="dataTabledetail"
                  :multi-select="false"
                  :showSerialNumber="false"
                  :dictMap="dictMap"
                  :queryParams="queryDetailParams"
                  :tableBindColumns="tableBindDetailColumns"
                  :opButtonList="opButtonList"
                  :reqConfig="regConfigDetail"
                  :showRightToolbar="false"
                  :initData="false"
                >
                  <template v-slot:operate>
                    <el-table-column label="操作" align="left" width="50" fixed="right">
                      <template slot-scope="scope">
                        <ht-button-group
                          :buttonList="[
                    {
                      label: '维 护',
                      permission: '',
                      doFunc: () => {
                        replydetial(scope.$index, scope.row);
                      },
                    },
                  ]"
                        />
                      </template>
                    </el-table-column>
                  </template>
                </ht-table>
              </div>
            </div>
          </div>
        </el-card>
      </el-form>
      <div slot="footer" class="dialog-footer" align="center">
        <el-button type="primary" @click="save">提 交</el-button>
      </div>
    </el-dialog>

    <el-dialog title="应答信息维护" center :visible.sync="opendetail" width="1024px" append-to-body :close-on-click-modal="false">
      <el-form ref="detailForm1" :model="detailForm1" :rules="replyrules" label-width="120px">
        <el-row type="flex">
          <el-col :span="12">
            <el-form-item label="账户账号(卡号)" prop="acctCardNo">
              <el-input v-model="detailForm1.acctCardNo" :disabled="true" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="账户名称" prop="acctnm">
              <el-input v-model="detailForm1.acctnm" :disabled="true" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex">
          <el-col :span="12">
            <el-form-item label="开户行行号" prop="acctBrno">
              <el-input v-model="detailForm1.acctBrno" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex">
          <el-col :span="12">
            <el-form-item label="公民身份号码校验结果" prop="ctznidvfyrslt">
              <el-select
                style="width: 100%"
                v-model="detailForm1.ctznidvfyrslt"
                placeholder="公民身份号码校验结果"
                @change="bizStatusChange"
              >
                <el-option
                  v-for="dict in dictMap.VERIFICATION_RTCODE"
                  :key="dict.value"
                  :label="`${dict.value}-${dict.text}`"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="电话/电挂校验结果" prop="telvfyrslt">
              <el-select style="width: 100%" v-model="detailForm1.telvfyrslt" placeholder="电话/电挂校验结果">
                <el-option
                  v-for="dict in dictMap.VERIFICATION_RTCODE"
                  :key="dict.value"
                  :label="`${dict.value}-${dict.text}`"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex">
          <el-col :span="12">
            <el-form-item label="业务状态" prop="bizStatus">
              <el-select style="width: 100%" v-model="detailForm1.bizStatus" placeholder="业务状态">
                <el-option
                  v-for="dict in dictMap.ACTNO_PROC_STATUS"
                  :key="dict.value"
                  :label="`${dict.value}-${dict.text}`"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <div v-if="this.detailForm1.bizStatus === 'PR05'">
          <el-row type="flex">
            <el-col :span="12">
              <el-form-item label="账户状态" prop="acctsts">
                <el-select style="width: 100%" v-model="detailForm1.acctsts" placeholder="账户状态">
                  <el-option
                    v-for="dict in dictMap.ACCOUNTSTATUSCODE"
                    :key="dict.value"
                    :label="`${dict.value}-${dict.text}`"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </div>

        <div v-if="this.detailForm1.bizStatus === 'PR09'">
          <el-row type="flex">
            <el-col :span="8">
              <el-form-item label="业务拒绝处理码" prop="bizRejectCode">
                <el-input
                  v-model="detailForm1.bizRejectCode"
                  placeholder="请输入业务拒绝处理码"
                  @change="getbase('REJECTCODE')"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-input v-model="detailForm1.bizRejectCodeDesc" :disabled="true" />
            </el-col>
            <el-col :span="4">
              <el-row>
                <el-button type="primary" icon="el-icon-search" plain @click="openBasedataDialog('REJECTCODE')">
                  请选择
                </el-button>
              </el-row>
            </el-col>
          </el-row>

          <el-row type="flex">
            <el-col :span="24">
              <el-form-item label="业务拒绝信息" prop="bizRejectInfo">
                <el-input type="textarea" v-model="detailForm1.bizRejectInfo" />
              </el-form-item>
            </el-col>
          </el-row>
        </div>
      </el-form>

      <div slot="footer" class="dialog-footer" align="center">
        <el-button type="primary" @click="replysave(detailForm1.index)">提 交</el-button>
      </div>
    </el-dialog>

    <base-data-dialog ref="basedataDialog" @confirm="confirmData"></base-data-dialog>
  </div>
</template>

<script>
import { listPageMixin } from "@/libs/listPageMixin";
import {
  apiUrlConfigEntry,
  apiUrlConfigEntryDetail,
  batchCustomerAccountReplyEntryDetail,
  batchCustomerAccountReplyEntrySave,
} from "@/api/cnaps/beps/395";
import {} from "@/api/cnaps/common/check";

import BaseDataDialog from "@/views/components/BasedataDialog";
import { getBasedata } from "@/api/cnaps/common/basedata";
import { AgentPayInOutApproveApprove } from "@/api/cnaps/beps/392";
import { checkRcvStBrnoStatus } from "@/api/cnaps/common/check";

export default {
  name: "BatchCustomerAccountReplyEntry",
  mixins: [listPageMixin],
  components: { BaseDataDialog },
  data() {
    return {
      // 查询参数
      queryParams: {
        startworkdate:'',
        endworkdate:'',
        startcpgdate:'',
        endcpgdate:'',
        idSeqno:'',
      },
      queryDetailParams: {},

      defaultEventKey: "defaultEventKey",
      loading: false,
      // 搜索
      formOptions: [],
      //
      searchFormDictList: [
        "PKG_STATUS",
        "TXN_STATUS",
        "PROCESSSTATUS",
        "ACTNO_PROC_STATUS",
        "ACCOUNTSTATUSCODE",
        "VERIFICATION_RTCODE",
      ],
      // 列信息
      regConfig: apiUrlConfigEntry,
      regConfigDetail: apiUrlConfigEntryDetail,
      tableBindColumns: [
        { prop: "id", label: "交易流水号" },
        { prop: "pkgId", label: "报文标识号" },
        { prop: "vdate", label: "CPG日期", type: "date" },
        { prop: "workdate", label: "工作日期", type: "date" },
        { prop: "acctCnt", label: "查询账户数目" },
        { prop: "rcvTime", label: "报文接收时间", type: "date" },
      ],

      tableBindDetailColumns: [
        { prop: "acctCardNo", label: "账户账号(卡号)" },
        { prop: "acctnm", label: "账户名称" },
        { prop: "acctBrno", label: "开户行行号" },
        { prop: "bizStatus", label: "业务状态", dictCode: "ACTNO_PROC_STATUS" },
        { prop: "ctznidnb", label: "公民身份号码" },
        { prop: "telephone", label: "电话/电挂" },
      ],
      dictMapAsync: {},
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      opendetail: false,
      // 表单参数
      detailForm: {
        id: undefined,
        pkgId: undefined,
        listCardNo: [],
      },
      detailForm1: {
        acctCardNo: undefined,
        acctnm: undefined,
        acctBrno: undefined,
        ctznidvfyrslt: undefined,
        telvfyrslt: undefined,
        bizStatus: undefined,
        acctsts: undefined,
        bizRejectCode: undefined,
        bizRejectCodeDesc: undefined,
        bizRejectInfo: undefined,
      },
      totalRecord: 0,
      totalPage: 0,
      needCount: 0,

      rules: {},

      replyrules: {
        acctBrno: [{ required: true, message: "请输入开户行行号", trigger: "blur" }],
        bizStatus: [{ required: true, message: "请输入业务状态", trigger: "blur" }],
        acctsts: [{ required: true, message: "请输入账户状态", trigger: "blur" }],
        bizRejectCode: [{ required: true, message: "请输入业务拒绝处理码", trigger: "blur" }],
        bizRejectInfo: [{ max: 105, message: "最长为105位", trigger: ['blur', 'change'] }],
        ctznidvfyrslt: [{ required: true, message: "请输入公民身份号码校验结果", trigger: "blur" }],
        telvfyrslt: [{ required: true, message: "请输入电话/电挂校验结果", trigger: "blur" }],
      },
    };
  },
  mounted() {
    const formOptions = [
      // 普通搜索

      {
        type: "dates",
        nameObj: {
            startTimeName: "startworkdate",
          endTimeName: "endworkdate",
        },
        defaultAttr: {
          size: "small",
          label: "工作日期",
          placeholder: ["请输入工作开始日期", "请输入工作结束日期"],
          format: "yyyy-MM-dd",
          valueFormat: "yyyyMMdd",
        },
      },

      {
        type: "dates",
        nameObj: {
          startTimeName: "startcpgdate",
          endTimeName: "endcpgdate",
        },
        defaultAttr: {
          size: "small",
          label: "CPG日期",
          placeholder: ["请输入CPG开始日期", "请输入CPG结束日期"],
          format: "yyyy-MM-dd",
          valueFormat: "yyyyMMdd",
        },
      },

      {
        type: "input",
        prop: "idSeqno",
        defaultAttr: {
          size: "small",
          label: "交易流水号",
          placeholder: "请输入交易流水号",
        },
      },
    ];
    this.formOptions = formOptions;
    this.dictMapAsync = this.getDictMap();
  },
  methods: {
    /***
     * 按钮操作汇总
     */
    /***
     * 按钮操作汇总
     */
    handleAction(type, row = {}) {
      switch (type) {
        case "detail":
          this.handleDetail(row);
          break;
      }
    },

    // 关闭按钮
    cancel() {
      this.open = false;
    },

    /** 详情按钮操作 */
    handleDetail(row) {
      this.title = "批量客户账户信息应答录入明细";
      batchCustomerAccountReplyEntryDetail(row).then((response) => {
        this.detailForm = response.result;
        this.queryDetailParams.id = response.result.id;
        this.open = true;
        this.$nextTick(() => {
          this.$refs.dataTabledetail.doQuery();
        });
      });
    },

    replydetial(index, row) {
      this.detailForm1 = Object.assign({}, row); //将数据传入dialog页面
      this.detailForm1.index = index; //传递当前index
      this.opendetail = true;
    },

    replysave(index) {
      this.$refs["detailForm1"].validate((valid) => {
        if (valid) {
          this.$refs.dataTabledetail.dataList[index].acctCardNo = this.detailForm1.acctCardNo;
          this.$refs.dataTabledetail.dataList[index].acctnm = this.detailForm1.acctnm;
          this.$refs.dataTabledetail.dataList[index].acctBrno = this.detailForm1.acctBrno;
          this.$refs.dataTabledetail.dataList[index].ctznidnb = this.detailForm1.ctznidvfyrslt;
          this.$refs.dataTabledetail.dataList[index].telephone = this.detailForm1.telvfyrslt;
          this.$refs.dataTabledetail.dataList[index].bizStatus = this.detailForm1.bizStatus;
          this.$refs.dataTabledetail.dataList[index].acctsts = this.detailForm1.acctsts;
          this.$refs.dataTabledetail.dataList[index].bizRejectCode = this.detailForm1.bizRejectCode;
          this.$refs.dataTabledetail.dataList[index].bizRejectCodeDesc = this.detailForm1.bizRejectCodeDesc;
          this.$refs.dataTabledetail.dataList[index].bizRejectInfo = this.detailForm1.bizRejectInfo;

          this.$refs.dataTabledetail.initColPer(this.$refs.dataTabledetail.dataList);
          this.opendetail = false;
        }
      });
    },

    openBasedataDialog(dataType = "") {
      this.$refs.basedataDialog.show(dataType);
    },

    confirmData(fromFlag, row) {
      this.$set(this.detailForm1, "bizRejectCode", row.dataValue);
      this.$set(this.detailForm1, "bizRejectCodeDesc", row.dataDesc);
    },
    //回车获取basedata值
    getbase(dataType) {
      const dataValue = this.detailForm1.bizRejectCode;
      getBasedata(dataType, dataValue)
        .then((response) => {
          if (response.code == "S" && response.result !== null) {
            this.$set(this.detailForm1, "bizRejectCode", response.result.dataValue);
            this.$set(this.detailForm1, "bizRejectCodeDesc", response.result.dataDesc);
          } else {
            this.msgSuccess("找不到对应的字典项");
            this.$set(this.detailForm1, "bizRejectCodeDesc", null);
          }
        })
        .catch((err) => {
          this.$set(this.detailForm1, "bizRejectCodeDesc", null);
        });
    },

    bizStatusChange() {
      if (this.detailForm1.bizStatus === "PR05") {
        this.$set(this.replyrules.bizRejectCode[0], "required", false);
      } else {
        this.$set(this.replyrules.bizRejectCode[0], "required", true);
      }
    },

    //重置按钮
    refresh() {
      this.resetForm("detailForm");
      this.$set(this.$refs.dataTabledetail, "dataList", []);
    },

    save() {

      let directSender = this.detailForm.directSender;
      if (directSender !== "") {
        checkRcvStBrnoStatus(directSender, "BEPS")
          .then((response) => {

          }).catch(err=>{
            return
        });
      }

      let count = this.isNotReply();
      if (count > 0) {
        this.msgError("还有" + count + "条记录未应答");
        return;
      }
      this.detailForm.listCardNo = this.$refs.dataTabledetail.dataList;
      this.$refs["detailForm"].validate((valid) => {
        if (valid) {
          batchCustomerAccountReplyEntrySave(this.detailForm)
            .then((response) => {
              this.loading = true;
              this.msgSuccess("提交成功");
              this.open = false;
              this.loading = false;
              this.$nextTick(() => {
                this.$refs.dataTable.doQuery(true);
              });
            })
            .catch((err) => {
              this.loading = false;
            });
        }
      });
    },

    isNotReply() {
      let StBrnodata = this.$refs.dataTabledetail.dataList;
      let j = 0;
      for (let i = 0; i < StBrnodata.length; i++) {
        let row = StBrnodata[i];
        if (row.bizStatus == undefined || row.bizStatus == "") {
          j++;
        }
      }

      return j;
    },
  },
};
</script>
